package de.baumann.browser.js;

import android.webkit.JavascriptInterface;

import com.google.gson.Gson;
import com.orhanobut.logger.Logger;

import de.baumann.browser.activitys.ReadArticleActivity;
import de.baumann.browser.api.net.vo.LoginInfo;
import de.baumann.browser.api.net.vo.PayParam;
import de.baumann.browser.base.BaseOdinActivity;
import de.baumann.browser.utils.UserDataKt;

/**
 * @author:
 * @date:2019/1/24 11:53
 * @discription: 为H5 提供用户数据 和 接收H5 回调
 */
public class AppJs {

    private BaseOdinActivity activity;

    public AppJs(BaseOdinActivity activity) {
        this.activity = activity;
    }

    /**
     * -----------------MALL-------------------------
     */
    private OnMallPayListener mallPayListener;

    public void setMallPayListener(OnMallPayListener mallPayListener) {
        this.mallPayListener = mallPayListener;
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    @JavascriptInterface
    public String getUserInfo() {
        Gson gson = new Gson();
        LoginInfo loginInfo = UserDataKt.getUser();
        loginInfo.setPassword("");
        Logger.d(gson.toJson(loginInfo));
        return gson.toJson(loginInfo);
    }

    /**
     * 支付回调
     *
     * @param trandNo
     */
    @JavascriptInterface
    public void pay(String trandNo,String amount) {
        if (mallPayListener != null) {
            mallPayListener.onOdinPay(trandNo,amount);
        }
    }

    @JavascriptInterface
    public void wechatPay(String payUrl) {
        if (mallPayListener != null) {
            PayParam payParam = new PayParam();
            payParam.setPayUrl(payUrl);
            mallPayListener.onWechatPay(payParam);
        }
    }



    /**
     * 商城中导航栏返回的回调
     */
    @JavascriptInterface
    public void back() {
        this.activity.finish();
    }

    public interface OnMallPayListener {
        void onOdinPay(String tradeNo, String amount);
        void onWechatPay(PayParam payParam);
    }

    /**
     * -----------------GAME-------------------------
     */
    @JavascriptInterface
    public String getUserData() {
        LoginInfo localInfo = UserDataKt.getUser();
        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setUser_id(localInfo.getUser_id());
        loginInfo.setToken(localInfo.getToken());
        loginInfo.setLicence(localInfo.getLicence());
        Gson gson = new Gson();
        return gson.toJson(loginInfo);
    }

    /**
     * 偷矿游戏中提供用户数据
     *
     * @return
     */
    @JavascriptInterface
    public String getStealUserData() {
        LoginInfo localInfo = UserDataKt.getUser();
        assert localInfo != null;
        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setUserId(localInfo.getUser_id());
        int stolenToken = activity.getIntent().getIntExtra("stolenId", 0);
        loginInfo.setStolenId(stolenToken);
        String s = new Gson().toJson(loginInfo);
        return s;
    }

    @JavascriptInterface
    public void login() {
//        activity.tokenInvalid();
//        activity.finish();
    }

    @JavascriptInterface
    public void shareArticle(){
        ((ReadArticleActivity)activity).shareArticle();
    }

    @JavascriptInterface
    public void shareArticleImage(String base64Img){
        ((ReadArticleActivity)activity).shareArticleImg(base64Img);
    }

}
